package com.dankegongyu.demo.ui

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView.OnItemClickListener
import android.widget.ArrayAdapter
import android.widget.PopupWindow
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.dankegongyu.demo.ui.layout.MaxHeightLayoutActivity
import com.dankegongyu.lib.ui.util.DKUIDisplayHelper
import com.dankegongyu.lib.ui.widget.popup.DKUINormalPopup
import com.dankegongyu.lib.ui.widget.popup.DKUIPopup
import com.dankegongyu.lib.ui.widget.popup.DKUIPopups
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn_dklayout.setOnClickListener { intent(DKLayoutActivity::class.java) }
        btn_maxHeightLayout.setOnClickListener { intent(MaxHeightLayoutActivity::class.java) }

    }

    private fun intent(cls: Class<*>) {
        startActivity(Intent(this@MainActivity, cls))
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        super.onCreateOptionsMenu(menu)
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.action_info -> showPopup(findViewById(R.id.action_info), true)
            R.id.action_add -> showPopup(findViewById(R.id.action_add), false)
        }
        return super.onOptionsItemSelected(item)
    }

    private fun showPopup(v: View, showArrow: Boolean) {
        var mNormalPopup: DKUIPopup? = null
        val listItems = mutableListOf<String>("Item 1", "Item 2", "Item 3", "Item 4", "Item 5",
                "Item 6", "Item 7", "Item 8")
        if (!showArrow) {
            listItems.add(0, "哈哈哈")
        }
        val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems)
        val onItemClickListener = OnItemClickListener { adapterView, view, i, l ->
            Toast.makeText(this@MainActivity, "Item " + (i + 1), Toast.LENGTH_SHORT).show()
            mNormalPopup?.dismiss()
        }
        mNormalPopup = DKUIPopups.listPopup(this,
                DKUIDisplayHelper.dp2px(this, 180),
                DKUIDisplayHelper.dp2px(this, 300),
                adapter,
                onItemClickListener)
                .shadow(true)
                .arrow(showArrow)
                .animStyle(if (showArrow) DKUINormalPopup.ANIM_AUTO else DKUINormalPopup.ANIM_GROW_FROM_CENTER)
                .edgeProtection(DKUIDisplayHelper.dp2px(this, 10))
                .offsetYIfTop(DKUIDisplayHelper.dp2px(this, 5))
                .onDismiss(PopupWindow.OnDismissListener {
//                    Toast.makeText(this, "onDismiss", Toast.LENGTH_SHORT).show()
                })
                .show(v)
    }
}